﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;

namespace QueryDemo
{
	/// <summary>
	/// Dumb control to manage the display of controls used within the web part.
	/// </summary>
	public partial class QueryDemoControl : UserControl
	{
		/// <summary>
		/// Data source of query results to display.
		/// </summary>
		public DataTable GridDataSource { get; set; }

		/// <summary>
		/// Type of query to display.
		/// </summary>
		public QueryType TypeOfQuery
		{
			set
			{
				queryType.Text = value.ToString();
			}
		}

		/// <summary>
		/// Title of list to display (if set).
		/// </summary>
		public string ListTitle
		{
			set
			{
				listTitle.Text = value;
			}
		}

		/// <summary>
		/// List ID to display (if set).
		/// </summary>
		public Guid ListId
		{
			set
			{
				listGuid.Text = (value == Guid.Empty ? String.Empty : value.ToString());
			}
		}

		/// <summary>
		/// Web ID to display (if set).
		/// </summary>
		public Guid WebId
		{
			set
			{
				webGuid.Text = (value == Guid.Empty ? String.Empty : value.ToString());
			}
		}

		/// <summary>
		/// Site collection ID to display (if set).
		/// </summary>
		public Guid SiteId
		{
			set
			{
				siteGuid.Text = (value == Guid.Empty ? String.Empty : value.ToString());
			}
		}

		/// <summary>
		/// Server template to display (if set).
		/// </summary>
		public int ServerTemplate
		{
			set
			{
				serverTemplate.Text = (value == 0 ? String.Empty : value.ToString());
			}
		}

		/// <summary>
		/// Text of CAML query to display (if set).
		/// </summary>
		public string CamlQuery
		{
			set
			{
				camlQuery.Text = value;
			}
		}

		/// <summary>
		/// Row limit to display.
		/// </summary>
		public int RowLimit
		{
			set
			{
				rowLimit.Text = value.ToString();
			}
		}

		/// <summary>
		/// Displays whether query is cross site capable.
		/// </summary>
		public bool CrossSiteCapable
		{
			set
			{
				crossSiteCapable.Text = value.ToString();
			}
		}

		/// <summary>
		/// Configures ObjectDataSource.
		/// </summary>
		protected override void CreateChildControls()
		{
			base.CreateChildControls();

			queryData.SelectMethod = "SelectDataTable";
			queryData.TypeName = GetType().AssemblyQualifiedName;
			queryData.ObjectCreating += QueryDataObjectCreating;

			if (!Page.IsPostBack)
			{
				GenerateDataColumns();
			}
		}

		/// <summary>
		/// Sets object instance to use for ObjectDataSource (this one).
		/// </summary>
		void QueryDataObjectCreating(object sender, ObjectDataSourceEventArgs e)
		{
			e.ObjectInstance = this;
		}

		/// <summary>
		/// Returns data for use by ObjectDataSource.
		/// </summary>
		/// <returns></returns>
		public DataTable SelectDataTable()
		{
			return GridDataSource;
		}

		/// <summary>
		/// Generates columns to enable search query results to be bound to ObjectDataSource.
		/// </summary>
		private void GenerateDataColumns()
		{
			foreach (DataColumn column in GridDataSource.Columns)
			{
				queryGrid.Columns.Add(new SPBoundField
				                      	{
				                      		DataField = column.ColumnName,
				                      		SortExpression = column.ColumnName,
				                      		HeaderText = column.ColumnName
				                      	});
			}
		}
	}
}
